package com.example.coinhelper.Activity;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;


import com.example.coinhelper.Bean.Details;
import com.example.coinhelper.Manage.DetailsManage;
import com.example.coinhelper.R;
import com.example.coinhelper.utils.MySesseion;
import com.example.coinhelper.utils.chart.LineChartManager;
import com.github.mikephil.charting.charts.LineChart;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;


public class AnalyticsActivity extends AppCompatActivity {

    private LineChart mLineChart;

    private LineChartManager mChartManager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_analytics);
        initView();
    }
    private void initView() {
        mLineChart = findViewById(R.id.line_chart);
        List<String> names = new ArrayList<>();
        List<Integer> colors = new ArrayList<>();
        names.add("支出");
        names.add("收入");
        colors.add(Color.RED);
        colors.add(Color.BLUE);
        mChartManager = new LineChartManager(mLineChart, names, colors);
        DetailsManage detailsManage = new DetailsManage(this);
        List<Details> detailsList = detailsManage.getAllDetailsByUsername(MySesseion.USER.getUsername());
        mChartManager.setDescription("收支曲线图"); // 设置描述信息
        LocalDate currentDate = LocalDate.now();
        // 计算本月的起始日期（向前推算29天）
        LocalDate startDate = currentDate.minusDays(29);
        float ymax = -10000,ymin = 100000;
        // 遍历近30天的日期
        for (LocalDate date = startDate; !date.isAfter(currentDate); date = date.plusDays(1)){
            double sumout = 0,sumin =0;
            for(int i = detailsList.size()-1;i>=0;i--){
                Details details = detailsList.get(i);
                LocalDateTime dateTime = LocalDateTime.parse(details.getDate(), DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm"));
                LocalDate dateTimeDate = dateTime.toLocalDate();
                boolean isSameDay = dateTimeDate.isEqual(date);
                if(details.getMoney()<0&&isSameDay){
                    sumout += details.getMoney();
                }else if(details.getMoney()>0&&isSameDay){
                    sumin += details.getMoney();
                }
            }
            List<Float> numbers = new ArrayList<>();
            numbers.add((float) -sumout);
            numbers.add((float) sumin);
            mChartManager.addEntry(numbers);
            ymax = (float) Math.max(ymax,-sumout);
            ymax = (float) Math.max(ymax,sumin);
            ymin = (float) Math.min(ymin,-sumout);
            ymin = (float) Math.min(ymin,sumin);
        }
        mChartManager.setYAxis(ymax+100, 0f, 5); // 设置Y轴范围和刻度数量
        mLineChart.invalidate(); // 最后更新图表
    }


}